package com.sz.framework.log;

import com.sz.framework.SzManager;
import com.sz.framework.config.SzConfig;
import com.sz.framework.utils.StrFormatterUtil;

/**
 * 控制台打印日志
 *
 * @author: yxl
 * 2025-07-22
 */
public class SzLogConsole implements SzLog {

    public static final int trace = 1;
    public static final int debug = 2;
    public static final int info = 3;
    public static final int warn = 4;
    public static final int error = 5;
    public static final int fatal = 6;

    public static String TRACE_PREFIX = "SzFramework [TRACE]-->: ";
    public static String DEBUG_PREFIX = "SzFramework [DEBUG]-->: ";
    public static String INFO_PREFIX = "SzFramework [INFO] -->: ";
    public static String WARN_PREFIX = "SzFramework [WARN] -->: ";
    public static String ERROR_PREFIX = "SzFramework [ERROR]-->: ";
    public static String FATAL_PREFIX = "SzFramework [FATAL]-->: ";

    /**
     * 日志输出的颜色
     */
    public static String DEFAULT_COLOR = "\033[39m"; //默认颜色（重置）
    public static String TRACE_COLOR = "\033[39m";
    public static String DEBUG_COLOR = "\033[34m"; //蓝色
    public static String INFO_COLOR = "\033[32m"; //绿色
    public static String WARN_COLOR = "\033[33m"; //黄色
    public static String ERROR_COLOR = "\033[31m"; //红色
    public static String FATAL_COLOR = "\033[35m"; //品红色（洋红）

    @Override
    public void trace(String str, Object... args) {
        println(trace, TRACE_COLOR, TRACE_PREFIX, str, args);
    }

    @Override
    public void debug(String str, Object... args) {
        println(debug, DEBUG_COLOR, DEBUG_PREFIX, str, args);
    }

    @Override
    public void info(String str, Object... args) {
        println(info, INFO_COLOR, INFO_PREFIX, str, args);
    }

    @Override
    public void warn(String str, Object... args) {
        println(warn, WARN_COLOR, WARN_PREFIX, str, args);
    }

    @Override
    public void error(String str, Object... args) {
        println(error, ERROR_COLOR, ERROR_PREFIX, str, args);
    }

    @Override
    public void error(String str, Throwable throwable) {
        println(error, ERROR_COLOR, ERROR_PREFIX, str, null);
        throwable.printStackTrace();
    }

    @Override
    public void fatal(String str, Object... args) {
        println(fatal, FATAL_COLOR, FATAL_PREFIX, str, args);
    }

    /**
     * 打印日志到控制台
     *
     * @param level  日志等级
     * @param color  颜色编码
     * @param prefix 前缀
     * @param str    字符串
     * @param args   参数列表
     */
    public void println(int level, String color, String prefix, String str, Object... args) {
        SzConfig config = SzManager.getConfig();
        if (config.getPrintLog() && level >= config.getLogLevel()) {
            if (config.getPrintColorLog() == Boolean.TRUE) {
                System.out.println(color + prefix + StrFormatterUtil.format(str, args) + DEFAULT_COLOR);
            } else {
                System.out.println(prefix + StrFormatterUtil.format(str, args));
            }
        }
    }

}
